using System;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using TakeAway.DataSet;

namespace TakeAway.Controls
{
    public partial class AssignEmployeeXtraForm : XtraForm
    {
        private Controller _controller;
        private string _shop;
        private bool _updating;

        public Controller Controller
        {
            set { _controller = value; }
        }

        public string Shop
        {
            set { _shop = value; }
        }

        public string EmployeeName { get; private set; }


        public AssignEmployeeXtraForm()
        {
            InitializeComponent();
            Load += AssignEmployeeXtraFormLoad;
            freeLookUpEdit.EditValueChanging += FreeLookUpEditEditValueChanging;
            assignedLookUpEdit.EditValueChanging += AssignedLookUpEditEditValueChanging;
            okSimpleButton.Click += OKSimpleButtonClick;
        }

        private void SetupFreeEmployeeData()
        {
            var dataSet = new FuncionarioDataSet();
            dataSet.Funcionario.AddFuncionarioRow("");
            dataSet.Merge(_controller.GetFreeEmployees(_shop));
            freeLookUpEdit.Properties.DataSource = dataSet.Funcionario;
            freeLookUpEdit.Properties.DisplayMember = dataSet.Funcionario.NomeColumn.ToString();
            freeLookUpEdit.Properties.ValueMember = dataSet.Funcionario.NomeColumn.ToString();
            freeLookUpEdit.Properties.Columns.Clear();
            freeLookUpEdit.Properties.Columns.AddRange(new[] { new LookUpColumnInfo("Nome", "Nome", 70, FormatType.None, "", true, HorzAlignment.Near) });
        }

        private void SetupAssignedEmployeeData()
        {
            var dataSet = new FuncionarioDataSet();
            dataSet.Funcionario.AddFuncionarioRow("");
            dataSet.Merge(_controller.GetAssignedEmployees(_shop));
            assignedLookUpEdit.Properties.DataSource = dataSet.Funcionario;
            assignedLookUpEdit.Properties.DisplayMember = dataSet.Funcionario.NomeColumn.ToString();
            assignedLookUpEdit.Properties.ValueMember = dataSet.Funcionario.NomeColumn.ToString();
            assignedLookUpEdit.Properties.Columns.Clear();
            assignedLookUpEdit.Properties.Columns.AddRange(new[] { new LookUpColumnInfo("Nome", "Nome", 70, FormatType.None, "", true, HorzAlignment.Near) });
        }


        void AssignEmployeeXtraFormLoad(object sender, EventArgs e)
        {
            _updating = true;
            SetupFreeEmployeeData();
            SetupAssignedEmployeeData();
            EmployeeName = "";
            _updating = false;
        }

        void FreeLookUpEditEditValueChanging(object sender, ChangingEventArgs e)
        {
            if (_updating) return;
            _updating = true;
            EmployeeName = e.NewValue == null ? "" : e.NewValue.ToString();
            assignedLookUpEdit.EditValue = null;
            _updating = false;
        }

        void AssignedLookUpEditEditValueChanging(object sender, ChangingEventArgs e)
        {
            if (_updating) return;
            _updating = true;
            EmployeeName = e.NewValue == null ? "" : e.NewValue.ToString();
            freeLookUpEdit.EditValue = null;
            _updating = false;
        }

        void OKSimpleButtonClick(object sender, EventArgs e)
        {
            if (EmployeeName == "")
                DialogResult = System.Windows.Forms.DialogResult.None;
        }

    }
}